#include "login.h"
#include "ui_login.h"
#include <QMessageBox>
#include <QDebug>
#include <QKeyEvent>
#include <QDesktopWidget>

Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login),m_home_page(NULL)
{
    ui->setupUi(this);
    m_lastClick = -1;
//    setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Tool );
    ui->comboBox_staff_ID->addItems(m_sql->inquire_staff_ID());
    ui->lineEdit_Password->setMaxLength(16);

    setWindowTitle(tr("    熠飞超市收银系统-登录界面"));
    this->setFixedSize(403,270);

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    ui->comboBox_staff_ID->installEventFilter(this);
    ui->lineEdit_Password->setText("000001");        //测试时使用
//    ui->lineEdit_Password->selectAll();

    ui->comboBox_staff_ID->installEventFilter(this);
    ui->lineEdit_Password->installEventFilter(this);
    ui->pushButton_Login->installEventFilter(this);
}

Login::~Login()
{
    delete ui;
}

bool Login::eventFilter(QObject *target, QEvent *event)
{
    if (target == ui->comboBox_staff_ID || target == ui->lineEdit_Password
     || target == ui->pushButton_Login)
    {
        if (event->type() == QEvent::FocusOut)
        {
            if (target == ui->lineEdit_Password)
            {
                m_lastClick = -1;
            }
        }
        //鼠标左键按下事件
        if (event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton)
            {
                if (target == ui->lineEdit_Password)
                {
                    if (m_lastClick != lineEdit_Password){
                        ui->lineEdit_Password->selectAll();
                        m_lastClick = lineEdit_Password;
                        return true;
                    }else if (m_lastClick == lineEdit_Password)
                    {
                        goto flag;
                    }
                }
            }
        }
        //键盘事件
        if (event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if (keyEvent->key() == Qt::Key_Escape)
            {
                this->hide();
                return true;
            }
            if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))
            {
                if (target == ui->comboBox_staff_ID)
                {
                    ui->lineEdit_Password->setFocus();
                    ui->lineEdit_Password->selectAll();
                    return true;
                }
                if (target == ui->lineEdit_Password)
                {
                    ui->pushButton_Login->setFocus();
                    return true;
                }
                if (target == ui->pushButton_Login)
                {
                    on_pushButton_Login_clicked();
                    return true;
                }
            }
        }
    }
flag:
    return QWidget::eventFilter(target, event);
}

void Login::resizeEvent(QResizeEvent *)
{
    this->setAutoFillBackground(true);

//    QPixmap pixmap(":/homepage/Image/homePage/background/ShowerPhotos.jpg");
//    QPixmap pixmap(":/homepage/Image/homePage/background/LinYunEr.jpg");
    QPixmap pixmap(":/homepage/Image/homePage/background/grassland.jpg");
//    QPixmap pixmap(":/homepage/Image/homePage/background/flower.jpg");
//    QPixmap pixmap(":/homepage/Image/homePage/background/gril.jpg");

    QPalette palette;
    palette.setBrush(QPalette::Window, QBrush(pixmap.scaled(size(),
                    Qt::IgnoreAspectRatio, Qt::SmoothTransformation)));
    setPalette(palette);
}

void Login::on_PushButton_Exit_clicked()
{
    close();
}

void Login::on_pushButton_Login_clicked()
{
    QString paswd = m_sql->inquire_staff_password(ui->comboBox_staff_ID->currentText());

    if(paswd == ui->lineEdit_Password->text())
    {
        if(m_home_page == NULL){
            m_home_page = new MainWindow;
            m_home_page->CreatStatusBar(ui->comboBox_staff_ID->currentText());
        }
        m_home_page->setWindowFlags(Qt::WindowStaysOnTopHint);
        m_home_page->show();
        m_home_page->move((QApplication::desktop()->width() - m_home_page->width())/2,
                         (QApplication::desktop()->height() - m_home_page->height())/2);

        if (m_sql->InquireSystemSettingsInformation().get_inventoryShortagesPrompts())
            m_home_page->displayStockRemind();

        this->hide();
    }
    else
    {
        QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                             tr("\n密码输入有误,请重新输入!"));
        ui->lineEdit_Password->clear();
        return;
    }
}

void Login::on_lineEdit_Password_returnPressed()
{
    on_pushButton_Login_clicked();
}
